package tech.yangzhijie.basic.knowledge.mianshi;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test11SyncCompateLock {

    public static void main(String[] args) {
        SyncAndLockDemo syncAndLockDemo = new SyncAndLockDemo();

        for (int i = 0; i < 3; i++) {
            final int num = i;
            new Thread(()->{
                switch (num){
                    case 0:for (int j = 0; j < 10; j++) syncAndLockDemo.printFive();break;
                    case 1:for (int j = 0; j < 10; j++) syncAndLockDemo.printTen();break;
                    case 2:for (int j = 0; j < 10; j++) syncAndLockDemo.printFifteen();break;
                }
            }).start();
        }
        /*new Thread(()->{
            for (int i = 0; i < 10; i++) {
                syncAndLockDemo.printFive();
            }
        }).start();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                syncAndLockDemo.printTen();
            }
        }).start();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                syncAndLockDemo.printFifteen();
            }
        }).start();*/
    }
}

class SyncAndLockDemo{

    private volatile int num = 1;

    private Lock lock = new ReentrantLock();
    private Condition condition1 = lock.newCondition();
    private Condition condition2 = lock.newCondition();
    private Condition condition3 = lock.newCondition();

    public void printFive(){
        lock.lock();
        try {
            while(num != 1){
                condition1.await();
            }
            for (int i = 1; i <= 5; i++) {
                System.out.println("线程: " + Thread.currentThread().getName() + "\t第 " + i + " 次\tnum: "+ num +"\tAA");
            }
            num = 2;
            condition2.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void printTen(){
        lock.lock();
        try {
            while(num != 2){
                condition2.await();
            }
            for (int i = 1; i <= 10; i++) {
                System.out.println("线程: " + Thread.currentThread().getName() + "\t第 " + i + " 次\tnum: "+ num +"\tBB");
            }
            num = 3;
            condition3.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void printFifteen(){
        lock.lock();
        try {
            while(num != 3){
                condition3.await();
            }
            for (int i = 1; i <= 15; i++) {
                System.out.println("线程: " + Thread.currentThread().getName() + "\t第 " + i + " 次\tnum: "+ num +"\tCC");
            }
            System.out.println("-------------------");
            num = 1;
            condition1.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}
